package org.penguin.study.bk.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.penguin.study.bk.mqtt.integration.MqttConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.DateUtils;

import java.util.Date;

/**
 * mqtt消息发送
 *
 * @author chengyinghua
 */
@RestController
@RequestMapping("/mqtt")
@ConditionalOnProperty(value = "mqtt.enabled", havingValue = "true")
@Slf4j
public class MqttController {

    private final MqttConfig.MyGateway myGateway;

    public MqttController(MqttConfig.MyGateway myGateway) {
        this.myGateway = myGateway;
    }

    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
    public String send(@RequestParam String topic, @RequestParam String message) {
        Date date = new Date();
        System.out.println("发送时间:" + DateUtils.formatISO(date));
        myGateway.sendToMqtt(topic, message, 2);
        return topic + "send message : " + message;
    }
}